'use strict';
const DB = require('../db/mysqlDB')
const User = DB.User;
const UserInfo = DB.UserInfo;
const ApiErrorNames = require('../error/ApiErrorNames')
const {handleError, handleSuccess} = require('../utlis/response')

class Auth {
    constructor(router) {
        console.log(router)
    }

    static async login(ctx) {
        let requestBody = ctx.request.body;
        let hasUser = await User.findOne({
            where: {name: requestBody.name},
            attributes: ['name']
        })
        if (hasUser) {
            let user = await User.findOne({
                where: {name: requestBody.name, password: requestBody.password},
                attributes: ['name', 'id', 'createdAt']
            })
            if (user) {
                return handleSuccess({ctx, data: user})
            } else {
                return handleError({ctx, error_name: ApiErrorNames.USER_LOGIN_ERROR})
            }
        } else {
            return handleError({ctx, error_name: ApiErrorNames.USER_LOGIN_ERROR})
        }
    }

    static async changepassword(ctx) {
        let requestBody = ctx.request.body;
        let user = await User.findOne({
            where: {id: requestBody.id, password: requestBody.oldPassword}
        })
        if (user) {
            let data = await  User.update(requestBody, {where: {id: requestBody.id}})
            return handleSuccess({ctx})
        } else {
            return handleError({ctx, error_name: ApiErrorNames.USER_PASSWORD_ERROR})
        }
    }

    static async register(ctx) {
        let requestBody = ctx.request.body;
        let user = await User.findOne({
            where: {name: requestBody.name},
            attributes: ['name', 'id', 'createdAt']
        })
        if (user) {
            return handleError({ctx, error_name: ApiErrorNames.USER_HAS_REGISTER})
        }
        await  User.create(requestBody)
        return handleSuccess({ctx, msg: '注册成功'})
    }

    static async createUserInfo(ctx) {
        let requestBody = ctx.request.body;
        let user = await UserInfo.findOne({where: {userId: requestBody.userId}})
        if (user) {
            await  UserInfo.update(requestBody, {where: {userId: requestBody.userId}})
            let data = await UserInfo.findOne({where: {userId: requestBody.userId}})
            return handleSuccess({ctx, data})
        } else {
            let data = await  UserInfo.create(requestBody)
            return handleSuccess({ctx, data})
        }
    }

    static async getInfo(ctx) {
        let params = ctx.params;
        let user = await User.findOne({
            where: {id: params.userId},
            attributes: ['name', 'id', 'createdAt']
        })
        if (user) {
            return handleSuccess({ctx, data: user})
        } else {
            return handleError({ctx, error_name: ApiErrorNames.USER_NOT_EXIST})
        }
    }

    static async getAllUser(ctx) {
        let query = ctx.query;
        let user = await User.findAll({
            where: {
                name: {$like: `${query.name}%`}
            },
            attributes: ['name', 'id', 'createdAt'],
            limit: query.limit || 10,

        })
        if (user) {
            return handleSuccess({ctx, data: user})
        } else {
            return handleError({ctx, error_name: ApiErrorNames.USER_NOT_EXIST})
        }
    }
}

module.exports = Auth;